package simbot.example.utils.hero;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;

import org.apache.commons.lang3.StringUtils;
import simbot.example.utils.client.HttpClientUtil;
import simbot.example.utils.hero.dto.getRoleDTO;
import simbot.example.utils.hero.dto.getlistDTO;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;


/**
 * @Author: 祺诺
 * @Name; 谢钱枫 1053557904@qq.com
 * @Date: 2021/6/9 15:05
 */
public class HeroUtil {

    private static String images = "https://300report.jumpw.com/static/images/";

    public static String getrole(String name) {
        String url = "http://300report.jumpw.com/api/getrole?name=" + name;
        //String s = HttpClientUtil.doGet(url);
        String s = HttpClientUtil.sendGet(url);
        getRoleDTO getRoleDTO = JSON.parseObject(s, getRoleDTO.class);
        if (ObjectUtil.isNull(getRoleDTO.getRole())) {
            return "查询的角色有误";
        }
        Role role = getRoleDTO.getRole();
        String roleResult = new String("角色名:" + role.getRoleName() + "|角色等级:" + role.getRoleLevel()
                + "|节操值:" + role.getJumpValue() + "|胜场数:" + role.getWinCount() + "|总胜场:" + role.getMatchCount() +
                "|更新日期:" + role.getUpdateTime());
      /*  String roleResult = "角色名:" + role.getRoleName() + "|角色等级:" + role.getRoleLevel()
                + "|节操值:" + role.getJumpValue() + "|胜场数:" + role.getWinCount() + "|总胜场:" + role.getMatchCount() +
                "|更新日期:" + role.getUpdateTime();*/
        //Rank(Type=1, RankName=团队实力排行, ValueName=实力值, Rank=43730, Value=1771, RankChange=469)
        String rankResult = null;
        if (ObjectUtil.isNull(getRoleDTO.getRank())) {
            return roleResult;
        }
        Rank[] rank = getRoleDTO.getRank();
        for (Rank r : rank) {
            if (StringUtils.equals(r.getRankName(), "团队实力排行")) {
                rankResult = "|团分:" + r.getValue();
            }
        }
        return roleResult + rankResult;
    }

    public static String getlist(String name) {
        String url = "http://300report.jumpw.com/api/getlist?name=" + name;
        //String s = HttpClientUtil.doGet(url);
        String s = HttpClientUtil.sendGet(url);
        getlistDTO getlistDTO = JSON.parseObject(s, getlistDTO.class);
        if (ObjectUtil.isNull(getlistDTO)) {
            return "未查询到数据";
        }
        PlayAgainstList[] list = getlistDTO.getList();
        StringBuilder result = new StringBuilder();
        for (PlayAgainstList playAgainstList : list) {
            result.append(//"比赛ID:" + playAgainstList.getMatchID() +
                    "|比赛类型:" + MatchType(playAgainstList.getMatchType()) +
                    "|英雄等级:" + playAgainstList.getHeroLevel() +
                    "|比赛结果:" + Result(playAgainstList.getResult())+
                    "丨比赛日期:" + playAgainstList.getMatchDate() +
                    "丨英雄名称:" + playAgainstList.getHero().getName()+"\n");
                    //"|英雄头像:" + images + playAgainstList.getHero().getIconFile() + "\n");
        }
        return result.toString();
    }

    /**
     * 字符集转码
     *
     * @param url
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String urlEncode(String url) throws UnsupportedEncodingException {
        if (url == null) {
            return null;
        }

        final String reserved_char = ";/?:@=&";
        String ret = "";
        for (int i = 0; i < url.length(); i++) {
            String cs = String.valueOf(url.charAt(i));
            if (reserved_char.contains(cs)) {
                ret += cs;
            } else {
                ret += URLEncoder.encode(cs, "utf-8");
            }
        }
        return ret.replace("+", "%20");
    }

    private static String MatchType(int matchType) {
        if (matchType == 1) {
            return "竞技场";
        } else if (matchType == 2) {
            return "战场";
        }
        return null;
    }

    private static String Result(int result) {
        if (result == 1) {
            return "胜利";
        } else if (result == 2) {
            return "失败";
        } else if (result == 3) {
            return "逃跑";
        }
        return null;
    }
}
